function [gbest,gbestval,fitcount]= PSO(fhd,Dimension,Particle_Number,Max_Gen,UpperBound,LowerBound,pos,FuncNo,boolPlot)
rand('state',sum(100*clock));
me=Max_Gen;
ps=Particle_Number;
D=Dimension;
cc=[2 2];   %acceleration constants
iwt=0.9-(1:me).*(0.5./me);

vel = zeros(ps, D);

e=feval(fhd,pos',FuncNo);
fitcount=ps;
pbest=pos;
pbestval=e; %initialize the pbest and the pbest's fitness value
[gbestval,gbestid]=min(pbestval);
gbest=pbest(gbestid,:);%initialize the gbest and the gbest's fitness value
gbestrep=repmat(gbest,ps,1);

if(boolPlot)
    figure(1)
    haxes = plot( 0 , 0 );
    XArray = [1];
    YArray = [gbestval];
    title('PSO');
end

for iter = 2 : me
    acc=cc(1).*rand(ps,D).*(pbest-pos)+cc(2).*rand(ps,D).*(gbestrep-pos);
    vel=iwt(i).*vel+acc;
    
    pos=pos+vel;
end

end